package com.pkutech.carcasher.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.AlertDialogWrapper;
import com.afollestad.materialdialogs.MaterialDialog;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.bean.BaseResponse;
import com.pkutech.carcasher.db.RealmManager;
import com.pkutech.carcasher.model.UserInfo;
import com.pkutech.carcasher.network.SoapApi;
import com.pkutech.carcasher.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;

/**
 * 个人中心
 * Created by cyy on 2016/6/11.
 */
public class UserDetailActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.name_layout)
    LinearLayout nameLayout;
    @BindView(R.id.sex_text)
    TextView sexText;
    @BindView(R.id.sex_layout)
    LinearLayout sexLayout;
    @BindView(R.id.age_text)
    TextView ageText;
    @BindView(R.id.age_layout)
    LinearLayout ageLayout;
    @BindView(R.id.tel_text)
    TextView telText;
    @BindView(R.id.tel_layout)
    LinearLayout telLayout;
    @BindView(R.id.account_layout)
    LinearLayout accountLayout;
    @BindView(R.id.logout_layout)
    LinearLayout logoutLayout;
    @BindView(R.id.account)
    TextView account;
    @BindView(R.id.birthday_text)
    TextView birthdayText;
    @BindView(R.id.birthday_layout)
    LinearLayout birthdayLayout;
    @BindView(R.id.address_text)
    TextView addressText;
    @BindView(R.id.address_layout)
    LinearLayout addressLayout;
    @BindView(R.id.jointime_text)
    TextView jointimeText;
    @BindView(R.id.jointime_layout)
    LinearLayout jointimeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);
        ButterKnife.bind(this);
        initToolbar(toolbar);

//        nameLayout.setOnClickListener(this);
//        sexLayout.setOnClickListener(this);
//        ageLayout.setOnClickListener(this);
//        telLayout.setOnClickListener(this);
//        accountLayout.setOnClickListener(this);
        logoutLayout.setOnClickListener(this);

        UserInfo userInfo = RealmManager.findUserInfo();
        account.setText(userInfo.getCode());
        username.setText(userInfo.getName());
        if(!TextUtils.isEmpty(userInfo.getSex()) && "1".equals(userInfo.getSex())){
            sexText.setText("男");
        }else if(!TextUtils.isEmpty(userInfo.getSex()) && "0".equals(userInfo.getSex())){
            sexText.setText("女");
        }
        if(!TextUtils.isEmpty(userInfo.getBirthday())){
            String[] birthday = userInfo.getBirthday().split(" ");
            if(birthday != null && birthday.length > 0){
                birthdayText.setText(birthday[0]);
            }
        }
        ageText.setText(String.valueOf(userInfo.getAge()));
        telText.setText(userInfo.getPhone());
        addressText.setText(userInfo.getAdress());
        if(!TextUtils.isEmpty(userInfo.getJointime())){
            String[] jointime = userInfo.getJointime().split(" ");
            if(jointime != null && jointime.length > 0){
                jointimeText.setText(jointime[0]);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//        if (id == R.id.editUser) {
//            return true;
//        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.name_layout:
                new MaterialDialog.Builder(this)
                        .title("姓名")
//                        .content(R.string.nick_name_label)
                        .inputType(InputType.TYPE_CLASS_TEXT)
                        .negativeText("取消")
                        .input("", username.getText().toString(), new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {
                                if (!TextUtils.isEmpty(input.toString().trim())) {
                                    username.setText(input.toString());
                                } else {
                                    ToastUtil.showInfoToast(UserDetailActivity.this, "姓名不能为空", ToastUtil.Position.TOP);
                                }
                            }
                        }).show();
                break;
            case R.id.sex_layout:
                int defaultSelect = -1;
                if (sexText.getText() != null && !TextUtils.isEmpty(sexText.getText().toString())) {
                    String sexStr = sexText.getText().toString().trim();
                    if (sexStr.equals("男")) {
                        defaultSelect = 0;
                    } else if (sexStr.equals("女")) {
                        defaultSelect = 1;
                    }
                }
                new MaterialDialog.Builder(this)
                        .title("性别")
                        .items(R.array.sex_single_choice_lable)
                        .itemsCallbackSingleChoice(defaultSelect, new MaterialDialog.ListCallbackSingleChoice() {
                            @Override
                            public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                sexText.setText(text.toString());
                                dialog.dismiss();
                                return true;
                            }
                        })
                        .show();
                break;
            case R.id.age_layout:
                new MaterialDialog.Builder(this)
                        .title("年龄")
//                        .content(R.string.nick_name_label)
                        .inputType(InputType.TYPE_CLASS_TEXT)
                        .negativeText("取消")
                        .input("", ageText.getText().toString(), new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {
                                if (!TextUtils.isEmpty(input.toString().trim())) {
                                    ageText.setText(input);
                                } else {
                                    ToastUtil.showInfoToast(UserDetailActivity.this, "昵称不能为空", ToastUtil.Position.TOP);
                                }
                            }
                        }).show();
                break;
            case R.id.tel_layout:
                new MaterialDialog.Builder(this)
                        .title("联系方式")
//                        .content(R.string.nick_name_label)
                        .inputType(InputType.TYPE_CLASS_TEXT)
                        .negativeText("取消")
                        .input("", telText.getText().toString(), new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {
                                if (!TextUtils.isEmpty(input.toString().trim())) {
                                    telText.setText(input);
                                } else {
                                    ToastUtil.showInfoToast(UserDetailActivity.this, "昵称不能为空", ToastUtil.Position.TOP);
                                }
                            }
                        }).show();
                break;
//            case R.id.car_num_layout:
//                new MaterialDialog.Builder(this)
//                        .title("车牌号")
////                        .content(R.string.nick_name_label)
//                        .inputType(InputType.TYPE_CLASS_TEXT)
//                        .negativeText("取消")
//                        .input("", carNumText.getText().toString(), new MaterialDialog.InputCallback() {
//                            @Override
//                            public void onInput(MaterialDialog dialog, CharSequence input) {
//                                if (!TextUtils.isEmpty(input.toString().trim())) {
//                                    carNumText.setText(input);
//                                } else {
//                                    ToastUtil.showInfoToast(UserDetailActivity.this, "昵称不能为空", ToastUtil.Position.TOP);
//                                }
//                            }
//                        }).show();
//                break;
//            case R.id.person_level_layout:
//                new MaterialDialog.Builder(this)
//                        .title("个人等级")
////                        .content(R.string.nick_name_label)
//                        .inputType(InputType.TYPE_CLASS_TEXT)
//                        .negativeText("取消")
//                        .input("", personLevelText.getText().toString(), new MaterialDialog.InputCallback() {
//                            @Override
//                            public void onInput(MaterialDialog dialog, CharSequence input) {
//                                if (!TextUtils.isEmpty(input.toString().trim())) {
//                                    personLevelText.setText(input);
//                                } else {
//                                    ToastUtil.showInfoToast(UserDetailActivity.this, "昵称不能为空", ToastUtil.Position.TOP);
//                                }
//                            }
//                        }).show();
//                break;
//            case R.id.account_layout:
//                new MaterialDialog.Builder(this)
//                        .title("个人等级")
////                        .content(R.string.nick_name_label)
//                        .inputType(InputType.TYPE_CLASS_TEXT)
//                        .negativeText("取消")
//                        .input("", accountText.getText().toString(), new MaterialDialog.InputCallback() {
//                            @Override
//                            public void onInput(MaterialDialog dialog, CharSequence input) {
//                                if (!TextUtils.isEmpty(input.toString().trim())) {
//                                    accountText.setText(input);
//                                } else {
//                                    ToastUtil.showInfoToast(UserDetailActivity.this, "昵称不能为空", ToastUtil.Position.TOP);
//                                }
//                            }
//                        }).show();
//                break;
            case R.id.logout_layout:
                new AlertDialogWrapper.Builder(this)
                        .setTitle(getString(R.string.please_note))
                        .setMessage("是否确认退出当前账号？")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                new Thread(new Runnable(){//相当于是实现runnable接口
                                    @Override
                                    public void run(){
                                        memlogout(RealmManager.findUserInfo().getCode());
                                    }
                                }).start();
                                userid=RealmManager.findUserInfo().getId();
                                loginFlag=1;
                                RealmManager.clearUserInfo();
                                dialog.dismiss();
                                Intent it = new Intent("finish");
                                sendBroadcast(it);
                                Intent intent = new Intent(UserDetailActivity.this, UserLoginActivity.class);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
                break;
        }
    }
    public void memlogout(String account){
        SoapApi.hmLogout(account)
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showSuccessToast(UserDetailActivity.this, "退出失败", ToastUtil.Position.TOP);
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if ("true".equals(baseResponse.getStatus())) {
                            ToastUtil.showSuccessToast(UserDetailActivity.this, "退出成功", ToastUtil.Position.TOP);
                        } else {
                            ToastUtil.showSuccessToast(UserDetailActivity.this, "退出失败", ToastUtil.Position.TOP);
                        }
                    }
                });
    }
}